home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / wrlib / tiff.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-14  |  3.3 KB  |  148 lines

  1. /* tiff.c - load TIFF image from file
  2.  * 
  3.  *  Raster graphics library
  4.  * 
  5.  *  Copyright (c) 1997 Alfredo K. Kojima
  6.  *
  7.  *  This library is free software; you can redistribute it and/or
  8.  *  modify it under the terms of the GNU Library General Public
  9.  *  License as published by the Free Software Foundation; either
  10.  *  version 2 of the License, or (at your option) any later version.
  11.  *  
  12.  *  This library is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  *  Library General Public License for more details.
  16.  *  
  17.  *  You should have received a copy of the GNU Library General Public
  18.  *  License along with this library; if not, write to the Free
  19.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  */
  21.  
  22. #include <config.h>
  23.  
  24.  
  25. #ifdef USE_TIFF
  26.  
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30.  
  31. #include <tiff.h>
  32. #include <tiffio.h>
  33.  
  34.  
  35. #include "wraster.h"
  36.  
  37.  
  38. RImage*
  39. RLoadTIFF(RContext *context, char *file, int index)
  40. {
  41.     RImage *image = NULL;
  42.     TIFF *tif;
  43.     int i;
  44.     unsigned char *r, *g, *b, *a;
  45.     uint16 alpha, amode;
  46.     uint32 width, height;
  47.     uint32 *data, *ptr;
  48.     uint16 extrasamples;
  49.     uint16 *sampleinfo;
  50.     int ch;
  51.     
  52.     
  53.     tif = TIFFOpen(file, "r");
  54.     if (!tif)
  55.       return NULL;
  56.  
  57.     /* seek index */
  58.     i = index;
  59.     while (i>0) {
  60.     if (!TIFFReadDirectory(tif)) {
  61.         RErrorCode = RERR_BADINDEX;
  62.         TIFFClose(tif);
  63.         return NULL;
  64.     }
  65.     i--;
  66.     }
  67.     
  68.     /* get info */
  69.     TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
  70.     TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
  71.     
  72.     TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
  73.               &extrasamples, &sampleinfo);
  74.     
  75.     alpha = (extrasamples == 1 &&
  76.          ((sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA) || (sampleinfo[0] == EXTRASAMPLE_UNASSALPHA)));
  77.     amode = (extrasamples == 1 && sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
  78.     
  79.     if (width<1 || height<1) {
  80.     RErrorCode = RERR_BADIMAGEFILE;
  81.     TIFFClose(tif);
  82.     return NULL;
  83.     }
  84.       
  85.     /* read data */
  86.     ptr = data = (uint32*)_TIFFmalloc(width * height * sizeof(uint32));
  87.     
  88.     if (!data) {
  89.     RErrorCode = RERR_NOMEMORY;
  90.     } else {
  91.     if (!TIFFReadRGBAImage(tif, width, height, data, 0)) {
  92.         RErrorCode = RERR_BADIMAGEFILE;
  93.     } else {
  94.         
  95.         /* convert data */
  96.         image = RCreateImage(width, height, alpha);
  97.        
  98.         if (alpha)
  99.         ch = 4;
  100.         else
  101.         ch = 3;
  102.         
  103.         if (image) {
  104.         int x, y;
  105.         
  106.         r = image->data;
  107.         g = image->data+1;
  108.         b = image->data+2;
  109.         a = image->data+3;
  110.  
  111.         /* data seems to be stored upside down */
  112.         data += width * (height-1);
  113.         for (y=0; y<height; y++) {
  114.             for (x=0; x<width; x++) {
  115.             
  116.             *(r) = (*data) & 0xff;
  117.             *(g) = (*data >> 8) & 0xff;
  118.             *(b) = (*data >> 16) & 0xff;
  119.  
  120.             if (alpha) {
  121.                 *(a) = (*data >> 24) & 0xff;
  122.                 
  123.                 if (amode && (*a > 0)) {
  124.                 *r = (*r * 255) / *(a);
  125.                 *g = (*g * 255) / *(a);
  126.                 *b = (*b * 255) / *(a);
  127.                 }
  128.                  
  129.                 a+=4;
  130.             }
  131.             
  132.             r+=ch; g+=ch; b+=ch;
  133.             data++;
  134.             }
  135.             data -= 2*width;
  136.         }
  137.         }
  138.     }
  139.     _TIFFfree(ptr);
  140.     }
  141.     
  142.     TIFFClose(tif);
  143.     
  144.     return image;
  145. }
  146.  
  147. #endif /* USE_TIFF */
  148.